package com.shared;

import java.io.DataOutputStream;
import java.io.Serializable;
import java.net.Socket;

/**
 * This is a Queue element including the Socket of the client
 * and the DataOutputStream.<p>
 * 
 * Note this class implements equal() and hashCode() methods which
 * has the following meaning:
 * Two queue elements are considered equal, if and only if, their socket
 * is equal. In other words, come from the same client. 
 * 
 * @author jiafanz
 *
 */
public class QueueElement implements Serializable
{
    private static final long serialVersionUID = 1L;
    
    private Socket                  socket;
    private DataOutputStream        outputStream;
    
    /**
     * Get the socket associated with this queue element.
     * @return a network socket object.
     */
    public Socket getSocket()
    {
        return socket;
    }
    
    /**
     * Set the socket associated with this queue element.
     * @param socket network socket object.
     */
    public void setSocket(Socket socket)
    {
        this.socket = socket;
    }
    
    /**
     * Get DataoutputStream object associated with this queue element.
     * @return a DataoutputStream object 
     */
    public DataOutputStream getOutputStream()
    {
        return outputStream;
    }
    
    /**
     * Set DataoutputStream object associated with this queue element.
     * @param outputStream DataoutputStream object 
     */
    public void setOutputStream(DataOutputStream outputStream)
    {
        this.outputStream = outputStream;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((socket == null) ? 0 : socket.hashCode());
        return result;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        QueueElement other = (QueueElement) obj;
        if (socket == null)
        {
            if (other.socket != null)
            {
                return false;
            }
        } 
        else if (!socket.equals(other.socket))
        {
            return false;
        }
        return true;
    }
}
